package com.onejson.ojmall.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import com.onejson.ojmall.common.domain.PageResult;
import com.onejson.ojmall.common.domain.ResponseDTO;
import com.onejson.ojmall.common.util.OjPageUtil;
import com.onejson.ojmall.dao.SRoleUserDao;
import com.onejson.ojmall.model.entity.SRoleUserEntity;
import com.onejson.ojmall.model.form.SRoleUserBatchForm;
import com.onejson.ojmall.model.form.SRoleUserForm;
import com.onejson.ojmall.model.form.SRoleUserQueryForm;
import com.onejson.ojmall.model.vo.SRoleUserVO;
import com.onejson.ojmall.model.vo.SRoleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * 角色用户功能表 Service 层
 *
 * @author wxx
 */
@Service
@Slf4j
public class SRoleUserService extends ServiceImpl<SRoleUserDao, SRoleUserEntity> {

    @Resource
    private SRoleUserDao sRoleUserDao;



    /**
     * 获取角色用户功能表列表
     */
    public PageResult<SRoleUserVO> getList(SRoleUserQueryForm queryForm) {
        Page<?> page = OjPageUtil.convert2PageQuery(queryForm);
        List<SRoleUserVO> list = sRoleUserDao.query(page, queryForm);
        return OjPageUtil.convert2PageResult(page, list);
    }

    /**
     * 查询角色用户功能表
     */
    public SRoleUserVO queryById(String detailId) {

        SRoleUserEntity detailEntity = sRoleUserDao.selectById(detailId);
        SRoleUserVO sRoleUserVO = new SRoleUserVO();

        try {
            BeanUtils.copyProperties(sRoleUserVO, detailEntity);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return sRoleUserVO;

    }


    /**
     * 【角色用户功能表】 - 添加
     */
    public ResponseDTO<String> addSRoleUser(SRoleUserForm addForm) {

        SRoleUserEntity entity = new SRoleUserEntity();

        addForm.setId(null);

        try {
            BeanUtils.copyProperties(entity, addForm);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

        sRoleUserDao.insert(entity);
        return ResponseDTO.ok();
    }

    /**
     * 【角色用户功能表】 - 更新
     */
    public ResponseDTO<String> updateSRoleUser(SRoleUserForm updateForm) {

        SRoleUserEntity entity = new SRoleUserEntity();

        try {
            BeanUtils.copyProperties(entity, updateForm);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        sRoleUserDao.updateById(entity);
        return ResponseDTO.ok();
    }


    /**
     * 【角色用户功能表】- 批量更新
     */
    public ResponseDTO<String> batchUpdateSRoleUser(SRoleUserBatchForm sRoleUserBatchForm) {

        SRoleUserEntity entity = new SRoleUserEntity();
        List<SRoleUserEntity> sRoleUserEntityList = new ArrayList<>();

        try {
            for (SRoleUserForm sRoleUserForm : sRoleUserBatchForm.getSRoleUserFormList()) {

                sRoleUserForm.setCreateTime(null);
                sRoleUserForm.setUpdateTime(null);
                BeanUtils.copyProperties(entity, sRoleUserForm);
                sRoleUserEntityList.add(entity);
            }
        } catch (Exception e) {
            System.out.println("错误消息");
            System.out.println(e.getMessage());
        }
        this.updateBatchById(sRoleUserEntityList);
        return ResponseDTO.ok();
    }


    /**
     * 批量删除
     */
    public ResponseDTO<String> batchDelete(List<String> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return ResponseDTO.ok();
        }

        sRoleUserDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     */
    public ResponseDTO<String> delete(String id) {
        if (null == id) {
            return ResponseDTO.ok();
        }

        sRoleUserDao.deleteById(id);
        return ResponseDTO.ok();
    }


    public List<SRoleVO> getRoleIdList(Long userId) {
        return sRoleUserDao.selectRoleByUserId(userId);
    }
}

